package util.translate;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class XMLJdomUtil {

	public void modify(String content) throws IOException, FileNotFoundException, JDOMException {
		SAXBuilder builder = new SAXBuilder();
		Document doc = builder.build(new FileInputStream("translator.xml"));
		Element element = doc.getRootElement();
		List list = element.getChildren("site");
		for (Iterator i = list.iterator(); i.hasNext();) {
			Element current = (Element) i.next();
			if (current.getChildText("dbname").equals(dbName)) {
				current.removeChild(xmlName);
				Element age = new Element(xmlName);
				age.setText(content);
				current.addContent(age);
			}			
		}
		Format format = Format.getCompactFormat();
		format.setEncoding("utf-8");
		format.setIndent(" ");
		XMLOutputter XMLOut = new XMLOutputter(format);
		XMLOut.output(doc, new FileOutputStream("translator.xml"));
	}
	
	String dbName = new String();

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}
	
	String xmlName = new String();

	public void setXmlName(String xmlName) {
		this.xmlName = xmlName;
	}

}